import math
import numpy as np
import matplotlib.pyplot as plt

a=-0.5
b=0
# error=0.0001
# delta=0.0001

# times=math.ceil(math.log((b-a)/error,2))



def fun(x):
    return 3*x*x-np.exp(x)

def fi(x):
    return math.sqrt(math.exp(x)/3)

#plt.style.use('seaborn-whitegrid')
plt.style.use('ggplot')

#绘制函数图和y=0处的红色虚线
x=np.linspace(a,b,1000)
#x=np.linspace(3.7,4,1000)

y=fun(x)
plt.axhline(y=0, color='red', linestyle='--')
plt.plot(x,y)



cnt=0
times=500

def iteration_cal(x):
    global times
    global cnt
    cnt+=1

    new_x=fi(x)
    if abs(x - new_x)<1e-4 or cnt==times or not(a<=new_x<=b):
        plt.scatter(new_x, fun(new_x), c='g')
        return new_x
    else:
        plt.scatter(new_x, fun(new_x), c='g')
        return iteration_cal(new_x)

print("%.3f"%iteration_cal((a+b)/2))

plt.show()